// 引入mockjs
const Mock = require('mockjs')

const List = []
const count = 100

for (let i = 0; i < count; i++) {
  const basePriceOptions = ['500', '800', '1200']
  const basePrice = basePriceOptions[Math.floor(Math.random() * basePriceOptions.length)]
  const clickNumber = Mock.Random.integer(1, 1500)
  const totalPrice = basePrice + clickNumber*5

  List.push(Mock.mock({
    id: '@increment',
    timestamp: +Mock.Random.date('T'),//操作日期
    operator: '@first',//操作人
    advertiser: '@cname(2,5)',//广告主
    title: '@ctitle(5, 10)',
    keywordCount: '@integer(1, 5)',
    basePrice: basePrice,
    clickNumber:clickNumber,
    type: '@pick(["Sport", "Food", "Play", "Traffic", "Study", "Daily"])',
    status:'@pick(["published","draft"])',
    totalPrice: totalPrice,
    display_time: '@datetime',
    img: '@image'
  }))
}

module.exports = [
  {
    url: '/XinMiao_MS/ad/list',
    type: 'get',
    response: config => {
      const { img,status, keywordCount,basePrice,totalPrice,clickNumber, type, title, page = 1, limit = 20, sort } = config.query

      let mockList = List.filter(item => {
        if (img && item.img !== img) return false
        if (status && item.status !== status) return false
        if (keywordCount && item.keywordCount !== keywordCount) return false
        if (basePrice && item.basePrice !== basePrice) return false
        if (totalPrice && item.totalPrice !== totalPrice) return false
        if (clickNumber && item.clickNumber !== clickNumber) return false
        if (type && item.type !== type) return false
        if (title && item.title.indexOf(title) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },

  {
    url: '/XinMiao_MS/ad/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      for (const ad of List) {
        if (ad.id === +id) {
          return {
            code: 20000,
            data: ad
          }
        }
      }
    }
  },

  {
    url: '/XinMiao_MS/ad/create',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },

  {
    url: '/XinMiao_MS/ad/update',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]

